import { _decorator, Component, Node, Animation, Vec2, v2, Collider2D, RigidBody2D, director, geometry, PhysicsSystem2D, ERaycast2DType, EPhysics2DDrawFlags, UI, UITransform, Vec3, Color, Graphics, Mask, math, BoxCollider2D, KeyCode, AnimationState, CCFloat } from 'cc';
import { StateDefine } from '../Config';
import { delay } from '../../Util/AsyncUtils';
import { Actor } from '../Actor';
import { SkillManager } from '../../Skill/SkillManager';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Actor {
    @property([CCFloat]) AttackMovement: number[] = [];
    
    @property JumpForce: number = 10;
    @property SwordReturnForce: number = 5;

    @property counterAttackDuration: number = 0.2;

    @property DashSpeed: number = 10;
    @property DashDuration: number = 10;
    DashDir: number = 1;

    isBusy: Boolean = false;

    pressedKeys: { [keyCode: number]: boolean } = {};
    private _input: Vec2 = v2();
    set input(v: Vec2) { this._input.set(v.x, v.y); }
    get input(): Vec2 { return this._input; }
    mousePos:Vec3;
    mouseInput: { [MouseEvent: number]: boolean } = {};

    
    Skill:SkillManager = null;
    sword:Node = null;

    start(): void {
        super.start()
        this.Skill = SkillManager.inst;
    }
    update(deltaTime: number) {
        super.update(deltaTime);
        this.checkForDashInput(deltaTime);
    }
    async BusyFor(n: number): Promise<void> {
        this.isBusy = true;
        await delay(n);
        this.isBusy = false;
    }
    checkForDashInput(dt: number) {
        if (this.pressedKeys[KeyCode.SHIFT_LEFT] && SkillManager.inst.dash_Skill.CanUseSkill()) {
            this.DashDir = this.input.x == 0 ? this.facingDir : this.input.x;
            this.stateMgr.transit(StateDefine.Dash);
        }
    }
    AssignNewSword(_sword:Node){
        this.sword = _sword;
    }
    CatchSword(){
        this.stateMgr.transit(StateDefine.CatchSword);
        this.sword.parent = null;
        this.sword = null
    }
}



